import React, { lazy } from "react";
import { createBrowserRouter } from "react-router-dom";
const ErrorPage = lazy(() => import("@/pages/404/"));
const Auth = lazy(() => import("@/components/auth"));

const router = createBrowserRouter([
  {
    path: "/",
    errorElement: <ErrorPage />,
    async lazy() {
      const { default: App } = await import("@/App");
      return { Component: App };
    },
    children: [
      {
        errorElement: <ErrorPage />,
        children: [
          {
            index: true,
            async lazy() {
              const { default: Home } = await import("@/pages/home/");
              return {
                element: (
                  <Auth>
                    <Home />
                  </Auth>
                )
              };
            }
          },
          {
            path: "collapse",
            async lazy() {
              const { default: Collapse } = await import("@/pages/collapse/");
              return {
                element: (
                  <Auth>
                    <Collapse />
                  </Auth>
                )
              };
            }
          },
          {
            path: "timeline",
            async lazy() {
              const { default: Timeline } = await import("@/pages/timeline/");
              return {
                element: (
                  <Auth>
                    <Timeline />
                  </Auth>
                )
              };
            }
          },
          {
            path: "app",
            async lazy() {
              const { default: Application } = await import(
                "@/pages/application/"
              );
              return {
                element: (
                  <Auth>
                    <Application />
                  </Auth>
                )
              };
            }
          }
        ]
      }
    ]
  },
  {
    path: "/login",
    async lazy() {
      const { default: Admin } = await import("@/pages/login/");
      return {
        element: (
          <Auth>
            <Admin />
          </Auth>
        )
      };
    }
  }
]);

export default router;
